package com.cy.store.controller;

import com.cy.store.service.ICartService;
import com.cy.store.service.ICartVOService;
import com.cy.store.vo.CartVO;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2025/3/1 14:11
 */
@RestController
@RequestMapping("carts")
public class CartController extends BaseController{
    @Autowired
    private ICartService cartService;
    @Autowired
    private ICartVOService cartVOService;
    @RequestMapping("add_to_cart")
    public JsonResult<Void> addToCart(@Param("productId") Integer pid, Long price, Integer num, HttpSession session){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        cartService.addToCart(uid,pid,num,username);
        return new JsonResult<>(OK);
    }
    @RequestMapping({"/",""})
    public JsonResult<List<CartVO>> getVOByUid(HttpSession session){
        Integer uid = getUidFromSession(session);
        List<CartVO> data = cartVOService.findByUid(uid);
        return new JsonResult<>(OK,data);
    }
    @RequestMapping("{cid}/num/add")
    public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid, HttpSession session){
        Integer uid = getUidFromSession(session);
        Integer num = cartService.addNum(cid, uid, getUsernameFromSession(session));
        return new JsonResult<>(OK,num);
    }
    @RequestMapping("list")
    public JsonResult<List<CartVO>> getVOByCids(Integer[] cids,HttpSession session){
        List<CartVO> data =cartVOService.getOVByCids(getUidFromSession(session)
                                   ,cids);
        return new JsonResult<>(OK,data);
    }

}
